<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8"/>
  <meta name="renderer" content="webkit"/>
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
  <meta http-equiv="Pragma" content="no-cache"/>
  <meta http-equiv="Cache-Control" content="no-cache,must-revalidate"/>
  <meta http-equiv="Expires" content="web,26 Feb 1997 08:21:57GMT"/>
  <meta http-equiv="Cache" content="no-cache"/>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
  <title>参数配置</title>
  <th:block th:include="common/pub_head :: default"></th:block>
  <th:block th:include="common/pub_head :: layui"></th:block>
</head>
<body class="layui-col-md-offset2 layui-col-md8">
<form action="/answerConfig/save" class="layui-form" lay-filter="config">
  <div class="layui-form-item">
    <div class="layui-inline">
      <label class="layui-form-label">单选</label>
      <div class="layui-input-inline" style="margin-right: 60px;margin-top: 10px;">
        <input type="text" class="layui-input" name="single" lay-verify="check" autocomplete="off">
      </div>
      <label class="layui-form-label">多选</label>
      <div class="layui-input-inline" style="margin-right: 60px;">
        <input type="text" class="layui-input" name="multi" lay-verify="check" autocomplete="off">
      </div>
      <label class="layui-form-label">填空</label>
      <div class="layui-input-inline" style="margin-right: 60px;">
        <input type="text" class="layui-input" name="fillBlank" lay-verify="check" autocomplete="off">
      </div>
      <label class="layui-form-label">判断</label>
      <div class="layui-input-inline" style="margin-right: 60px;">
        <input type="text" class="layui-input" name="judge" lay-verify="check" autocomplete="off">
      </div>
      <label class="layui-form-label">简答</label>
      <div class="layui-input-inline" style="margin-right: 60px;">
        <input type="text" class="layui-input" name="answer" lay-verify="check" autocomplete="off">
      </div>
    </div>
  </div>
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button class="layui-btn" lay-submit lay-filter="*">提交</button>
      <button type="button" class="layui-btn layui-btn-primary" onclick="returnAnswer();">关闭</button>
    </div>
  </div>
</form>
<script type="text/javascript" th:inline="javascript">
  /*<![CDATA[*/
  function returnAnswer() {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
  }

  var info = [[${config}]];
  var id = [[${id}]];
  var single = 5;
  var multi = 5;
  var fillBlank = 5;
  var judge = 5;
  var answer = 5;
  if (info.length != 0) {
    single = info[0].questionAmount;
    multi = info[1].questionAmount;
    fillBlank = info[2].questionAmount;
    judge = info[3].questionAmount;
    answer = info[4].questionAmount;
  }
  $(function () {
    layui.use(['form'], function () {
      var form = layui.form;
      form.verify({
        check: function (value, item) {
          if (!new RegExp("^(([1-9])|([1-4][0-9])|50)$").test(value)) {
            return '请输入1到50之间的整数。';
          }
        }
      });
      form.val('config', {
        "single": single,
        "multi": multi,
        "fillBlank": fillBlank,
        "judge": judge,
        "answer": answer
      });
      form.on('submit(*)', function (data) {
        var result = data.field;
        if (info.length == 0) {
          info = new Array(5);
          info[0] = {"cardId": id, "answerType": "SINGLE", "questionAmount": result.single};
          info[1] = {"cardId": id, "answerType": "MULTI", "questionAmount": result.multi};
          info[2] = {"cardId": id, "answerType": "FILL_BLANK", "questionAmount": result.fillBlank};
          info[3] = {"cardId": id, "answerType": "JUDGE", "questionAmount": result.judge};
          info[4] = {"cardId": id, "answerType": "ANSWER", "questionAmount": result.answer};
          $.ajax({
            url: path + '/answerConfig/save',
            data: JSON.stringify(info),
            contentType: 'application/json',
            type: 'post',
            success: function (data) {
              layer.msg('配置成功！', {icon: 6, time: 1000}, function () {
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
              });
            }
          });
        } else {
          info[0].questionAmount = result.single;
          info[1].questionAmount = result.multi;
          info[2].questionAmount = result.fillBlank;
          info[3].questionAmount = result.judge;
          info[4].questionAmount = result.answer;
          $.ajax({
            url: path + '/answerConfig/update',
            data: JSON.stringify(info),
            contentType: "application/json",
            type: 'post',
            success: function (data) {
              layer.msg('配置成功！', {icon: 6, time: 1000}, function () {
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index);
              });
            }
          });
        }
        return false;
      })
    })
  })
  /*]]>*/
</script>
</body>
</html>
